<?php

namespace Biz\service\wiki;


use Biz\model\brand\Brand;

/**
 * Class WikiContentValid
 * @package Biz\service\wiki
 * @method static cancelSelectedContent($id, $brand_id)
 * @method static selectedContent($id, $brand_id)
 * @method static reclaimContent($id, $brand_id)
 * @method static cancelContent($id, $brand_id)
 * @method static ignoreWikiContent($id, $brand_id)
 * @method static cancelWikiContent($id, $brand_id)
 * @method static claimWikiContent($id, $brand_id)
 * @method static claimWiki($id, $brand_id)
 * @method static ignoreWiki($id, $brand_id)
 * @method static cancelWiki($id, $brand_id)
 * @method static ignoreContent($id, $brand_id)
 */
class WikiContentValid
{

    const VALID_CONTENT_ALLOW_STATUS_MAP = [
        'cancelSelectedContent' => [
            \Biz\model\brand\Content::CLAIM_CONTENT_DONE,
        ],
        'selectedContent' => [
            \Biz\model\brand\Content::CLAIM_CONTENT_DONE,
        ],
        'reclaimContent' => [
            \Biz\model\brand\Content::CLAIM_CONTENT_CANCEL,
        ],
        'cancelContent' => [
            \Biz\model\brand\Content::CLAIM_CONTENT_DONE,
        ],
        'claimWikiContent' => [
            \Biz\model\brand\Content::CLAIM_CONTENT_DONE,
            \Biz\model\brand\Content::CLAIM_CONTENT_MATCH,
        ],
        'ignoreWikiContent' => [
            \Biz\model\brand\Content::CLAIM_CONTENT_DONE,
            \Biz\model\brand\Content::CLAIM_CONTENT_IGNORE,//忽略的内容还能不能操作
            \Biz\model\brand\Content::CLAIM_CONTENT_MATCH,
        ],
        'cancelWikiContent' => [
            \Biz\model\brand\Content::CLAIM_CONTENT_DONE,
        ],
        'ignoreContent' => [
            \Biz\model\brand\Content::CLAIM_CONTENT_MATCH
        ],
    ];

    const VALID_WIKI_ALLOW_STATUS_MAP = [
        'ignoreWiki' => [
            \Biz\model\brand\ContentWiki::RELATION_MATCH,
        ],
        'claimWiki' => [
            \Biz\model\brand\ContentWiki::RELATION_MATCH,
        ],
        'cancelWiki' => [
            \Biz\model\brand\ContentWiki::RELATION_DONE,
        ],
    ];


    public static function author($brandId)
    {
        $brand_type = Brand::get('brand_type', ['id' => $brandId]);
        return in_array($brand_type, [Brand::BRAND_TYPE_BEST, Brand::BRAND_TYPE_SUPER]);
    }

    public static function checkClaim($content, $wiki_num = 0)
    {
        $self = $content['brand_uid'] > 0;
        $wikiNum = $content['wiki_num'] > $wiki_num;
        $content = $content['content_status'] == \Biz\model\brand\Content::CLAIM_MATCH;
        return $self || $wikiNum || $content;
    }

    public static function checkMatch($content)
    {
        $wikiStatus = $content['wiki_status'] > 0;
        $content = $content['content_status'] == \Biz\model\brand\Content::CLAIM_WAIT_MATCH;
        return $wikiStatus || $content;
    }

    public static function validWikiCondition($cid)
    {
        return [
            'cid' => $cid,
            'relation' => [
                \Biz\model\brand\ContentWiki::RELATION_DONE,
            ],
            'audit_status' => [
                \Biz\model\brand\Content::AUDIT_STATUS_SUCCESS,
                \Biz\model\brand\Content::AUDIT_STATUS_AUTO,
            ],
        ];
    }

    public static function waitAuditWikiCondition($cid)
    {
        return [
            'cid' => $cid,
            'relation' => [
                \Biz\model\brand\ContentWiki::RELATION_DONE,
            ],
            'audit_status' => \Biz\model\brand\Content::AUDIT_STATUS_NONE,
        ];
    }

    /**
     * 内容操作权限判断
     * @param $id
     * @param $brand_id
     * @param $allowStatus
     * @return array|mixed
     * @throws \Exception
     */
    protected static function validContent($id, $brand_id, $allowStatus)
    {
        //检查内容是否存在
        $content = \Biz\model\brand\Content::get('*', [
            'brand_id' => $brand_id,
            'id' => $id,
        ]);
        if (!$content) throw new \Exception('操作的内容不存在,id:' . $id);

        if ($allowStatus && !in_array($content['claim_content_status'], $allowStatus)) {
            //当前状态
            $desc = \Biz\model\brand\Content::CLAIM_CONTENT_DESC_MAP[$content['claim_content_status']];
            throw new \Exception('内容的状态不正确,当前状态为:' . $desc);
        }

        return $content;
    }

    /**
     * 商品操作权限判断
     * @param $id
     * @param $brand_id
     * @param $allowStatus
     * @return array|mixed
     * @throws \Exception
     */
    protected static function validRelateWiki($id, $brand_id, $allowStatus)
    {
        //查询wiki信息
        $wiki = \Biz\model\brand\ContentWiki::get('*', [
            'id' => $id,
            'brand_id' => $brand_id,
        ]);

        if (!$wiki) throw new \Exception('操作的商品不存在');

        if ($allowStatus && !in_array($wiki['relation'], $allowStatus)) throw new \Exception('商品的状态不正确');

        return $wiki;
    }


    /**
     * 校验入口
     * @param $name
     * @param $arguments
     * @return array|mixed
     * @throws \Exception
     */
    public static function __callStatic($name, $arguments)
    {
        if (isset(self::VALID_CONTENT_ALLOW_STATUS_MAP[$name])) {
            //self::LOG_MAP[$name]
            array_push($arguments, self::VALID_CONTENT_ALLOW_STATUS_MAP[$name]);

            return self::validContent(...$arguments);
        }

        if (isset(self::VALID_WIKI_ALLOW_STATUS_MAP[$name])) {
            //self::LOG_MAP[$name]
            array_push($arguments, self::VALID_WIKI_ALLOW_STATUS_MAP[$name]);

            return self::validRelateWiki(...$arguments);
        }
        return null;
    }
}
